﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Newsletter;

namespace blackhouse.Aparts.Web.Pages {
	public partial class Newsletter : NonCmsBasePage {

		#region Fields

		private Guid confirmGuid;
		private Guid removeGuid;
		private Factory factory;

		#endregion

		#region Event Handlers

		protected override void OnPreInit(EventArgs e) {
			this.AllowSkinOverride = true;
			HttpContext.Current.Items.Add("skin", "blackhouse");

			base.OnPreInit(e);
		}

		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			this.ReadParameters();
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

			this.Execute();
		}

		#endregion

		#region Methods

		private void ReadParameters() {
			string sConfirmGuid, sRemoveGuid;
			sConfirmGuid = Request["ConfirmGuid"];
			sRemoveGuid = Request["RemoveGuid"];
			if (!string.IsNullOrEmpty(sConfirmGuid))
				Guid.TryParse(sConfirmGuid, out this.confirmGuid);
			if (!string.IsNullOrEmpty(sRemoveGuid))
				Guid.TryParse(sRemoveGuid, out this.removeGuid);
		}

		private void Execute() {
			this.factory = Factory.Instance();
			ISubscriber subscriber;
			if (this.confirmGuid != null) {
				subscriber = this.factory.GetSubscriber(this.confirmGuid);
				if (subscriber.Exist()) {
					subscriber.Confirm();
					this.ltlConfirmOk.Text = string.Format(this.ltlConfirmOk.Text, subscriber.Email);
					this.ltlConfirmOk.Visible = true;
					return;
				}
			}
			if (this.removeGuid != null) {
				subscriber = this.factory.GetSubscriber(this.removeGuid);
				if (subscriber.Exist()) {
					subscriber.Remove();
					this.ltlRemoveOk.Text = string.Format(this.ltlRemoveOk.Text, subscriber.Email);
					this.ltlRemoveOk.Visible = true;
					return;
				}
			}
			this.ltlEmailNotFound.Visible = true;
		}

		#endregion

	}
}